using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node struct_declarator in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnstruct_declarator : IFullFormatter {
        // [117]=struct_declarator : declarator ;
        // [118]=struct_declarator : ':' constant_expression ;
        // [119]=struct_declarator : declarator ':' constant_expression ;

        private readonly TokenRange _scope;
        private readonly Vndeclarator? r2;
        private readonly Token? r1;
        private readonly Vnconstant_expression? r0;

        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            if (r2 != null) {
                context.PrintBlanksAnd(r2, preConfig, writer);
                if (r1 != null && r0 != null) {
                    var config = new BlankConfig();
                    context.PrintCommentsBetween(r2, r1, config, writer);
                    context.PrintBlanksAnd(r1, config, writer);
                    context.PrintCommentsBetween(r1, r0, config, writer);
                    context.PrintBlanksAnd(r0, config, writer);
                }
            }
            else if (r1 != null && r0 != null) {
                context.PrintBlanksAnd(r1, preConfig, writer);
                var config = new BlankConfig();
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            else { throw new NotImplementedException(); }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        // [119]=struct_declarator : declarator ':' constant_expression ;
        // dumped by DefaultExtractor
        internal Vnstruct_declarator(Vndeclarator? r2, Token? r1, Vnconstant_expression? r0) {
            this.r2 = r2;
            this.r1 = r1;
            this.r0 = r0;
            if (r2 != null) {
                if (r1 != null && r0 != null) { this._scope = new TokenRange(r2, r0); }
                else { this._scope = new TokenRange(r2); }
            }
            else if (r1 != null && r0 != null) { this._scope = new TokenRange(r1, r0); }
            else { throw new NotImplementedException(); }
        }

    }
}
